<template>
  <div class="demo">
    <div class="demo-title">自定义上传列表</div>
    <div class="demo-content">
      <Upload
        v-model:file-list="fileList"
        name="file"
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
        :headers="headers"
        @change="handleChange"
      >
        <Button><UploadOutlined />Click to Upload</Button>
        <template #itemRender="{ file, actions }">
          <Space>
            <span :style="file.status === 'error' ? 'color: red' : ''">{{ file.name }}</span>
            <a href="javascript:;" @click="actions.download">download</a>
            <a href="javascript:;" @click="actions.remove">delete</a>
          </Space>
        </template>
      </Upload>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { UploadOutlined } from '@ant-design/icons-vue';
  import Button from '@sscd/button';
  import Message from '@sscd/message';
  import Space from '@sscd/space';
  import Upload from '@sscd/upload';
  import type { UploadChangeParam, UploadProps } from '@sscd/upload';
  const fileList = ref<UploadProps['fileList']>([
    {
      uid: '-1',
      name: 'xxx.png',
      status: 'done',
      url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
      thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    },
    {
      uid: '-2',
      name: 'yyy.png',
      status: 'done',
      url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
      thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    },
    {
      uid: '3',
      name: 'zzz.png',
      status: 'error',
      response: 'Server Error 500', // custom error message to show
      url: 'http://www.baidu.com/zzz.png',
    },
  ]);
  const headers = {
    authorization: 'authorization-text',
  };
  const handleChange = (info: UploadChangeParam) => {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      Message.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      Message.error(`${info.file.name} file upload failed.`);
    }
  };
</script>
